package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.MessageBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.glide.GlideApp;
import com.qiantu.phone.ui.fragment.MessageListFragment;
import com.qiantu.phone.widget.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * time   : 2021/12/22
 */
public final class MessageCenterActivity extends AppActivity {

    private String[] tabs = new String[]{"全部", "智能", "警报"};
    private int pageIndex = 1;
    private int pageSize = 10;
    /**
     * 全部 : 0
     * 智能 : 1
     * 警报 : 2
     * 通知 : 3
     */
    private int category = 3;

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, MessageCenterActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private View btnNotice;
    private TextView tvNotice;
    private ImageView iconNotice;
    private ViewPager viewPager;
    private ViewPagerIndicator tabBar;

    @Override
    protected int getLayoutId() {
        return R.layout.message_center_activity;
    }

    @Override
    protected void initView() {
        btnNotice = findViewById(R.id.btn_notice);
        tvNotice = findViewById(R.id.tv_notice);
        iconNotice = findViewById(R.id.icon_notice);
        viewPager = findViewById(R.id.view_pager);
        tabBar = findViewById(R.id.tab_bar);
        initViewPager();
        setOnClickListener(R.id.btn_notice);
    }

    private void initViewPager() {
        List<Fragment> fragments = new ArrayList<>();
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return tabs.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return null;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
//                super.destroyItem(container, position, object);
            }
        });
        tabBar.setupViewpager(viewPager);
        tabBar.setTitles(tabs);
        fragments.add(MessageListFragment.newInstance(0));
        fragments.add(MessageListFragment.newInstance(1));
        fragments.add(MessageListFragment.newInstance(2));
        viewPager.setCurrentItem(0);
    }

    @Override
    protected void initData() {
        //获取通知消息
        LLHttpManager.getMessages(this, pageIndex, pageSize, category, AppApplication.getInstance().getCurrentHouseSerialNo(), new HttpCallback<HttpData<MessageBean>>(this) {

            @Override
            public void onStart(Call call) {
//                showDialog();
            }

            @Override
            public void onSucceed(HttpData<MessageBean> result) {
                super.onSucceed(result);
                if (result != null && result.getData().getList() != null && result.getData().getList().size() > 0) {
                    MessageBean.ListDTO notice = result.getData().getList().get(0);
                    tvNotice.setText(notice.getTitle());
                    tvNotice.setEllipsize(TextUtils.TruncateAt.MARQUEE);
                    tvNotice.setMarqueeRepeatLimit(-1);
                    tvNotice.setSelected(true);
                    tvNotice.setSingleLine(true);
                    tvNotice.setFocusable(true);
                    tvNotice.setFocusableInTouchMode(true);
                    GlideApp.with(iconNotice).load(notice.getIcon())
                            .error(iconNotice.getDrawable())
                            .placeholder(iconNotice.getDrawable())
                            .into(iconNotice);
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
//                hideDialog();
            }
        });
    }

    @Override
    public void onRightClick(View view) {
        Intent intent = new Intent(getContext(), MessageSettingActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_notice:
                Intent intent = new Intent(getContext(), NoticeListActivity.class);
                startActivity(intent);
                break;
        }
    }
}